//  我的-部门管理-投递热度表
const baseUrl='https://gsdse.cn/api/KJRecruit';
const db = wx.cloud.database({
  env: 'cloud1-8g54d6xb74e96e7d'
})
const _ = db.command

/* 全局变量 */
var dh = 'hotdata[0].num'
var ms = 'hotdata[1].num'
var pc = 'hotdata[2].num'
var ui = 'hotdata[3].num'
var wl = 'hotdata[4].num'
var xmt = 'hotdata[5].num'
var ys = 'hotdata[6].num'

var compare = function (prop) { //prop是一种属性
  /* 降序排列 */
  return function (obj1, obj2) {
    var a = obj1[prop]; //prop在这里指.num
    var b = obj2[prop];
    if (a > b) {
      return -1;
    } else if (a < b) {
      return 1;
    } else {
      return 0;
    }
  }
}

Page({
  data: {
    //查看的年份数据的开始时间戳
    lookStartTimeStamp:"",
    //查看的年份数据的结束时间戳
    lookEndTimeStamp:"",
    hotdata: [{
        title: "动画部",
        num: 0
      },
      {
        title: "秘书处",
        num: 0
      },
      {
        title: "PC技术部",
        num: 0
      },
      {
        title: "UI设计部",
        num: 0
      },
      {
        title: "网络研发部",
        num: 0
      },
      {
        title: "新媒体运营部",
        num: 0
      },
      {
        title: "影视部",
        num: 0
      },
    ],
    hot: [],
    n: 0,
    year: [2022, 2023, 2024, 2025, 2026],
    yearnum: '',
    year_index: '',
  },

  _goback() {
    wx.navigateBack({
      delta: 1,
      fail: res => {
        console.log(res);
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },

  /* 下拉框 */
 _ChooseYear: function (e) {
    /* console.log('picker发送选择改变，携带下标为', e.detail.value) */
    console.log('picker发送选择改变，携带值为', this.data.year[e.detail.value])
    //yearnum为请求数据时的年份
    this.setData({
      yearnum: this.data.year[e.detail.value],
      year_index: e.detail.value,
      lookStartTimeStamp:this.getYearTimeStamp( this.data.year[e.detail.value],1,1,0,0,0),
      lookEndTimeStamp:this.getYearTimeStamp(this.data.year[e.detail.value],12,31,23,59,59)
    })
    console.log("时间戳",this.data.lookStartTimeStamp);
    console.log("时间戳",this.data.lookEndTimeStamp);
    this.getCurrentYearAllDataAndCount();
  },

  getDate() {
    let that = this;
    wx.showLoading({
      title: '计算数据中',
    })
    var date = new Date();
    var Y = date.getFullYear();
    console.log(Y);
    console.log('今年是', Y, '年')
  
    this.setData({
      yearnum: Y,
      lookStartTimeStamp:this.getYearTimeStamp(Y,1,1,0,0,0),
      lookEndTimeStamp:this.getYearTimeStamp(Y,12,31,23,59,59)
    });

    this.getCurrentYearAllDataAndCount();
    console.log("时间戳",this.data.lookStartTimeStamp);
    console.log("时间戳",this.data.lookEndTimeStamp);
  },

  //获取年份时间戳
  getYearTimeStamp(year,month,date,hours,minute,seconds){
    var specificDate = new Date();
    specificDate.setFullYear(year);
    specificDate.setMonth(month-1); // 月份是从 0 开始的，所以 11 表示 12 月
    specificDate.setDate(date);
    specificDate.setHours(hours);
    specificDate.setMinutes(minute);
    specificDate.setSeconds(seconds);
    return specificDate.getTime();
  },

  //投递数量排序
  async getCurrentYearAllDataAndCount() {
    //临时统计表
    let hot = [{
        title: "秘书处",
        num: 0
      },
      {
        title: "网络研发部",
        num: 0
      },
      {
        title: "影视部",
        num: 0
      },
      {
        title: "动画部",
        num: 0
      },
      {
        title: "PC技术部",
        num: 0
      },
      {
        title: "新媒体运营部",
        num: 0
      },
      {
        title: "UI设计部",
        num: 0
      },
    ];
    //获取每个部门的志愿数量
    for (let i = 0; i < hot.length; i++) {
      const num = await this.getWillingCount(i + 1)
      //console.log(num)
      hot[i].num = num
    }
    //冒泡排序
    for (var i = 0; i < hot.length - 1; i++) {
      for (var j = 0; j < hot.length - 1 - i; j++) {
        if (hot[j].num < hot[j + 1].num) {
          var temp = hot[j];
          hot[j] = hot[j + 1];
          hot[j + 1] = temp;
        }
      }
    }
    this.setData({
      hot: hot
    })
    wx.hideLoading()
  },

  //获取每个部门的志愿数量
  getWillingCount(departmentId) {
    const that=this;
    return new Promise((reslove, reject) => {
      wx.request({
        url: baseUrl+'/countWilling',
        method: 'POST',
        data: {
          department: departmentId,
          startTime:that.data.lookStartTimeStamp,
          endTime:that.data.lookEndTimeStamp
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          reslove(res.data.data)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})